package com.mall.market.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mall.market.mapper.SmsHomeRecommendSubjectMapper;
import com.mall.model.domain.SmsHomeRecommendSubject;
import com.mall.model.dto.SmsHomeRecommendSubjectDto;
import com.mall.model.vo.CommonPage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class SmsHomeRecommendSubjectService extends ServiceImpl<SmsHomeRecommendSubjectMapper, SmsHomeRecommendSubject> {
    @Autowired(required = false)
    private SmsHomeRecommendSubjectMapper smsHomeRecommendSubjectMapper;

    /**
     * 分页查询
     *
     * @param dto
     * @return
     */
    public CommonPage list(SmsHomeRecommendSubjectDto dto) {
        Page<SmsHomeRecommendSubject> pageInfo = new Page<>(dto.getPageNum(), dto.getPageSize());
        LambdaQueryWrapper<SmsHomeRecommendSubject> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(dto.getSubjectName() != null, SmsHomeRecommendSubject::getSubjectName, dto.getSubjectName());
        queryWrapper.eq(dto.getRecommendStatus() != null, SmsHomeRecommendSubject::getRecommendStatus, dto.getRecommendStatus());
        Integer count = smsHomeRecommendSubjectMapper.selectCount(queryWrapper);
        smsHomeRecommendSubjectMapper.selectPage(pageInfo, queryWrapper);
        return new CommonPage(dto.getPageNum(), dto.getPageSize(), count, pageInfo.getRecords());
    }

    //增
    public void create(List<SmsHomeRecommendSubject> smsHomeRecommendSubjectList) {
        this.saveBatch(smsHomeRecommendSubjectList);
    }

    //删
    public void delete(List ids) {
        smsHomeRecommendSubjectMapper.deleteBatchIds(ids);
    }

    //修改状态
    public void updateStatus(List<Integer> ids, Integer recommendStatus) {
        LambdaQueryWrapper<SmsHomeRecommendSubject> wrapper = new LambdaQueryWrapper<>();
        wrapper.in(SmsHomeRecommendSubject::getId, ids);
        List<SmsHomeRecommendSubject> smsHomeRecommendSubjects = smsHomeRecommendSubjectMapper.selectList(wrapper);
        for (SmsHomeRecommendSubject smsHomeRecommendSubject : smsHomeRecommendSubjects) {
            if (smsHomeRecommendSubject != null) {
                smsHomeRecommendSubject.setRecommendStatus(recommendStatus);
                smsHomeRecommendSubjectMapper.updateById(smsHomeRecommendSubject);
            }
        }
    }

    //修改排序
    public void updateSort(Integer id, Integer sort) {
        SmsHomeRecommendSubject smsHomeRecommendSubject = smsHomeRecommendSubjectMapper.selectById(id);
        if (smsHomeRecommendSubject != null) {
            smsHomeRecommendSubject.setSort(sort);
            smsHomeRecommendSubjectMapper.updateById(smsHomeRecommendSubject);
        }
    }


}

